package cn.yunhe.exception;

import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class GlobalExceptionReslover implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        CustomerException customerException = null;
        ModelAndView modelAndView = new ModelAndView();
        if(ex instanceof CustomerException){
            customerException = (CustomerException) ex;
            modelAndView.addObject("error",customerException.getMessage());
            modelAndView.setViewName("error");
        }else if(ex instanceof UnauthorizedException){
            // 无权限访问异常
            ex.printStackTrace();
            modelAndView.addObject("error","权限不足");
            modelAndView.setViewName("noauth");
        }else {

            ex.printStackTrace();
            // 如果 抛出的异常 不是 自定义异常 或者 是咱们不了解的异常

            modelAndView.addObject("error","未知异常，请联系系统管理员");
            modelAndView.setViewName("error");
        }

        return modelAndView;
    }
}
